package com.liik.bomb.screen.action;

import com.liik.game.mvc.tiled.object.Entity;
import com.liik.game.mvc.tiled.object.MovableEntity;
import com.liik.game.mvc.tiled.object.Tile;
import com.liik.game.mvc.tiled.object.Tile.Type;
import com.liik.game.mvc.tiled.TiledGameModel;
import com.liik.game.mvc.tiled.TiledGameModel.Environment;

public class BombermanEntity extends MovableEntity {

	public static final int NOT_SET = -1;

	float startX, startY;
	public int lives;
	public int bombRange;
	public int bombCount;
	public long deathTime;
	public long invincibleTime;

	public BombermanEntity() {
		super(.07f, .3f, 30);
	}

	public void initialize(float startX, float startY, int lives, int bombRange, int bombCount) {
		this.startX = startX;
		this.startY = startY;
		this.x = startX;
		this.y = startY;
		this.lives = lives;
		this.bombRange = bombRange;
		this.bombCount = bombCount;
		this.deathTime = NOT_SET;
		this.invincibleTime = System.currentTimeMillis() + 1000;
	}

	@Override
	public boolean isActive() {
		return true;
	}

	@Override
	public void update(TiledGameModel gameModel, long currentTimeMillis) {
		if (deathTime != NOT_SET && deathTime < currentTimeMillis) {
			lives--;
			x = startX;
			y = startY;
			deathTime = NOT_SET;
			invincibleTime = currentTimeMillis + 1000;
		}
		if (lives == -1) {
			// GAME OVER
		}
	}

	@Override
	public boolean canWalk(Tile tile) {
		return tile.type == Type.Empty;
	}

	@Override
	public boolean canMove() {
		return deathTime == NOT_SET;
	}

	@Override
	protected boolean isBlockedByEntity(Entity entity) {
		return entity instanceof BombEntity;
	}

	@Override
	public void handleCollision(Entity otherEntity, TiledGameModel gameModel, long currentTimeMillis) {
		if (otherEntity instanceof EnemyEntity) {
			kill(currentTimeMillis);
		}
	}

	@Override
	public void handleEnvironment(Environment environment, TiledGameModel gameModel, long currentTimeMillis) {
		switch (environment) {
		case Air:
			break;
		case Fire:
			kill(currentTimeMillis);
			break;
		case Water:
			break;
		}
	}

	private void kill(long currentTimeMillis) {
		if (this.deathTime == NOT_SET) {
			this.deathTime = currentTimeMillis + 1000;
		}
	}

	@Override
	public void handleDefaultAction(TiledGameModel gameModel, long currentTimeMillis) {
		BombGameController.plantBomb(gameModel);
	}
}
